package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.GraphInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.web.app.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.app.mapper.RoomInfoMapper;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.FacilityInfoService;
import com.atguigu.lease.web.app.service.GraphInfoService;
import com.atguigu.lease.web.app.service.LabelInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo> implements ApartmentInfoService {

    @Autowired
    private LabelInfoService labelInfoService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private FacilityInfoService facilityInfoService;

    /**
     * 根据房间id获取其所属公寓详细信息
     *
     * @param id 公寓id
     * @return 公寓详细信息
     */
    @Override
    public ApartmentItemVo getApartmentItemVoById(Long id) {

        // 公寓基础信息
        ApartmentInfo apartmentInfo = getById(id);
        // 公寓标签信息
        List<LabelInfo> labelInfoList = labelInfoService.getLabelListByApartmentId(id);
        // 公寓图片信息
        List<GraphInfo> graphInfoList = graphInfoService.getGraphListByApartmentId(id);
        // 公寓房间最低租金
        BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(id);

        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setGraphVoList(graphInfoList);
        apartmentItemVo.setMinRent(minRent);

        // 返回公寓基本信息
        return apartmentItemVo;
    }

    /**
     * 根据公寓id获取公寓详细信息
     *
     * @param id 公寓id
     * @return 公寓详细信息
     */
    @Override
    public ApartmentDetailVo getApartmentDetailVoById(Long id) {
        // 获取不是很详细的公寓信息
        ApartmentItemVo apartmentItemVo = getApartmentItemVoById(id);
        // 创建详细的公寓信息Vo对象
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        // 复制已有的公寓信息到详细公寓信息Vo对象
        BeanUtils.copyProperties(apartmentItemVo, apartmentDetailVo);

        // 处理图片信息： ApartmentItemVo 和 ApartmentDetailVo 的图片信息对象不同，Item里面的图片信息反而要比Detail信息里面的图片更详细
        ArrayList<GraphVo> graphVoList = new ArrayList<>();
        apartmentItemVo.getGraphVoList().forEach(graphVo -> {
            graphVoList.add(new GraphVo(graphVo.getName(), graphVo.getUrl()));
        });

        // 装配图片信息
        apartmentDetailVo.setGraphVoList(graphVoList);

        // 获取公寓配套信息列表
        List<FacilityInfo> facilityInfoList = facilityInfoService.getFacilityInfoListByApartmentId(id);
        // 获取公寓的房间最便宜的租金
        BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(id);
        // 获取公寓是否删除字段
        Byte isDeleted = apartmentDetailVo.getIsDeleted();


        // 装配公寓配套信息列表
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        // 装配公寓的房间最便宜的租金
        apartmentDetailVo.setMinRent(minRent);
        // 装配公寓是否被删除
        apartmentDetailVo.setIsDelete(isDeleted == 1);


        return apartmentDetailVo;
    }
}




